"ui";
let { config } = require('./config.js')(runtime, this)
let singletonRequire = require('./lib/SingletonRequirer.js')(runtime, this)
let runner = require('./core/JingDongBeans2.js')
let notifyOper = runner.notifyOper()
let loopCounter = runner.loopCounter
console.log(loopCounter.getValue())
 
// ================== 界面部分 ==================
ui.layout( 
  <vertical padding="16">
      <text textSize="18sp" textColor="#000000" text="推送 Token 设置" />
      
      <text textSize="14sp" marginTop="12">PushPlus Token</text>
      <input id="pushPlusInput" hint="请输入 PushPlus Token" inputType="text" />
      
      <text textSize="14sp" marginTop="12">WxPusher Token</text>
      <input id="wxpusherInput" hint="请输入 WxPusher Token" inputType="text" />
      
      <text textSize="14sp" marginTop="12">选择发送通道</text>
      <radiogroup id="channelGroup">
          <radio id="onlyPushPlus" text="PushPlus"/>
          <radio id="onlyWxPusher" text="WxPusher"/>
      </radiogroup>

      <text textSize="14sp" marginTop="12">日志上传站点</text>
      <input id="logSite" hint="请输入" inputType="text" />

      <text textSize="14sp" marginTop="12">站点Token</text>
      <input id="logToken" hint="请输入" inputType="text" />

      <text textSize="14sp" marginTop="12">上传目录</text>
      <input id="logDir" hint="请输入" inputType="text" />

      <text textSize="14sp" marginTop="12">任务执行次数</text>
      <input id="loopCount" hint="请输入" inputType="text" />

      <text textSize="14sp" marginTop="12">任务执行最大次数</text>
      <input id="loopMaxCount" hint="请输入" inputType="text" />

      <text textSize="14sp" marginTop="12">执行任务数量</text>
      <input id="taskCount" hint="请输入" inputType="text" />
      
      <button id="saveBtn" text="保存" w="*" marginTop="16"/>
      <text id="status" text="" textColor="#388E3C" textSize="12sp" marginTop="8"/>
  </vertical>
);
 
// ================== 逻辑部分 ==================
 
// 读取已有配置并回填 UI 
function loadConfig() {
  ui.logSite.setText(notifyOper.tokenStore.getValue().logSite||'');
  ui.logToken.setText(notifyOper.tokenStore.getValue().logToken||'');
  ui.logDir.setText(notifyOper.tokenStore.getValue().logDir||'');

  ui.pushPlusInput.setText(notifyOper.tokenStore.getValue().pushPlusToken);
  ui.wxpusherInput.setText(notifyOper.tokenStore.getValue().wxpusherToken);
  switch (notifyOper.tokenStore.getValue().channel)  {
    case "pushplus":
        ui.onlyPushPlus.checked  = true;
        break;
    case "wxpusher":
        ui.onlyWxPusher.checked  = true;
        break;
  }
  ui.loopCount.setText(loopCounter.getValue().count ? loopCounter.getValue().count.toString() : '0')
  ui.loopMaxCount.setText(loopCounter.getValue().maxCount ? loopCounter.getValue().maxCount.toString() : '20')
  ui.taskCount.setText(JSON.stringify(loopCounter.getValue()))
}
 
// 保存配置 
function saveConfig() {
  notifyOper.tokenStore.updateStorageValue(storeValue => {
    storeValue.pushPlusToken = ui.pushPlusInput.text().trim()
    storeValue.wxpusherToken = ui.wxpusherInput.text().trim()
    storeValue.channel = ui.onlyPushPlus.checked  ? "pushplus" :
                ui.onlyWxPusher.checked  ? "wxpusher" : "pushplus"

    storeValue.logSite = ui.logSite.text().trim()
    storeValue.logToken = ui.logToken.text().trim()
    storeValue.logDir = ui.logDir.text().trim()
  })
  loopCounter.updateStorageValue(storeValue => {
    storeValue.count = parseInt(ui.loopCount.text().trim())
    storeValue.maxCount = parseInt(ui.loopMaxCount.text().trim())
  })
  // notifyOper.appendMsg('消息结束')
  // notifyOper.sendNotifyMsg('测试')
}
 
// 绑定按钮 
ui.saveBtn.click(()  => {
    saveConfig();
    toast("配置已保存");
});
 
// 首次加载回显 
loadConfig();
